package com.ctsi.ssdc.admin.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

import com.ctsi.ssdc.admin.domain.dto.CscpUserUpdateDTO;
import com.zdww.biyi.component.sdk.aop.BeanExposeMethodAble;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctsi.ssdc.admin.domain.CscpUserDetailLike;
import com.ctsi.ssdc.admin.domain.dto.CscpUserDetailCriteria;
import com.ctsi.ssdc.admin.domain.dto.CscpUserDetailDTO;
import com.ctsi.ssdc.admin.service.CscpUserDetailService;
import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.security.SecurityUtils;

import com.ctsi.ssdc.util.Base64Util;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;

import static com.ctsi.ssdc.admin.consts.ComponentConstant.ADMIN;
import static com.ctsi.ssdc.admin.consts.ComponentConstant.METHOD;


/**
 * REST controller for managing CscpUserDetail.
 *
 * @author ctsi biyi generator
 *
 */

@RestController
@RequestMapping("/api/system")
public class CscpUserDetailController {

    private final Logger log = LoggerFactory.getLogger(CscpUserDetailController.class);

    private static final String ENTITY_NAME = "cscpUserDetail";

    private final CscpUserDetailService cscpUserDetailService;

    public CscpUserDetailController(CscpUserDetailService cscpUserDetailService) {
        this.cscpUserDetailService = cscpUserDetailService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
    
    /**
     * POST  /cscpUserDetails : Create a new cscpUserDetail.
     *
     * @param cscpUserDetailDTO the cscpUserDetailDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cscpUserDetailDTO, or with status 400 (Bad Request) if the cscpUserDetail has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cscpUserDetails")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpUserDetailDTO> createCscpUserDetail(@RequestBody CscpUserDetailDTO cscpUserDetailDTO) throws URISyntaxException {
        log.debug("REST request to save CscpUserDetail : {}", cscpUserDetailDTO);
        if (cscpUserDetailDTO.getId() != null) {
            throw new BadRequestAlertException("A new cscpUserDetail cannot already have an ID", ENTITY_NAME, "idexists");
        }
        CscpUserDetailDTO result = cscpUserDetailService.insert(cscpUserDetailDTO);
        return ResponseEntity.created(new URI("/api/cscpUserDetails/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cscpUserDetails : Updates an existing cscpUserDetail.
     *
     * @param cscpUserUpdateDTO the cscpUserDetailDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cscpUserDetailDTO,
     * or with status 400 (Bad Request) if the cscpUserDetailDTO is not valid,
     * or with status 500 (Internal Server Error) if the cscpUserDetailDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cscpUserDetails")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpUserDetailDTO> updateCscpUserDetail(@RequestBody CscpUserUpdateDTO cscpUserUpdateDTO) throws Exception {
        log.debug("REST request to update CscpUserDetail : {}", cscpUserUpdateDTO);
        //解决水平越权的问题2020/12/10
        if("0".equals(cscpUserUpdateDTO.getStatus())){
            if(SecurityUtils.getCurrentUserId() != 1){
                if(SecurityUtils.getCurrentUserId() != cscpUserUpdateDTO.getCscpUserDetailDTO().getUserId()){
                    throw new BadRequestAlertException("该用户无权限",ENTITY_NAME,"idexists");
                }
            }
        }
        //验证base64图片，防止XSS攻击
        if(StringUtils.isNotBlank(cscpUserUpdateDTO.getCscpUserDetailDTO().getImgPath())) {
        	if(!Base64Util.validateBASE64(cscpUserUpdateDTO.getCscpUserDetailDTO().getImgPath())) {
        		throw new BadRequestAlertException("The imgPath of cscpUserDetail pattern error", ENTITY_NAME, "imgPath");
        	}
        }
        if (cscpUserUpdateDTO.getCscpUserDetailDTO().getId() == null) {
            return createCscpUserDetail(cscpUserUpdateDTO.getCscpUserDetailDTO());
        }
        CscpUserDetailDTO result = cscpUserDetailService.userPwdUpdate(cscpUserUpdateDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, cscpUserUpdateDTO.getCscpUserDetailDTO().getId().toString()))
            .body(result);
    }

    /**
     * GET  /cscpUserDetails : get the cscpUserDetails.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cscpUserDetails in body
     */
    @GetMapping("/cscpUserDetails")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public PageResult<CscpUserDetailDTO> getCscpUserDetails(CscpUserDetailDTO cscpUserDetailDTO, Pageable page) {
        log.debug("REST request to get CscpUserDetails");
        return cscpUserDetailService.findByCscpUserDetailDTO(cscpUserDetailDTO, page);
    }
    
    /**
     * GET  /cscpUserDetails : get the cscpUserDetails.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cscpUserDetails in body
     */
    @GetMapping("/cscpUserDetailsByCriteria")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public PageResult<CscpUserDetailDTO> getCscpUserDetailsByCriteria(CscpUserDetailCriteria cscpUserDetailCriteria, Pageable page) {
        log.debug("REST request to get CscpUserDetailsByCriteria");
        return cscpUserDetailService.findByCscpUserDetailCriteria(cscpUserDetailCriteria, page);
    }

    /**
     * GET  /cscpUserDetails/:id : get the "id" cscpUserDetail.
     *
     * @param id the id of the cscpUserDetailDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cscpUserDetailDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cscpUserDetails/{id}")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpUserDetailDTO> getCscpUserDetail(@PathVariable Integer id) {
        log.debug("REST request to get CscpUserDetail : {}", id);
        CscpUserDetailDTO cscpUserDetailDTO = cscpUserDetailService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(cscpUserDetailDTO));
    }
    
    /**
     * GET  /cscpUserDetails/:id : get the "id" cscpUserDetail.
     *
     * @return the ResponseEntity with status 200 (OK) and with body the cscpUserDetailDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cscpCurrentUserDetails")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<CscpUserDetailDTO> getCscpUserDetails() {
        
    	int uid = SecurityUtils.getCurrentUserId();
    	
        log.debug("REST request to get CscpUserDetail by userId : {}", uid);
        
        CscpUserDetailDTO cscpUserDetailDTO = cscpUserDetailService.findByUserId(uid);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(cscpUserDetailDTO));
    }

    /**
     * DELETE  /cscpUserDetails/:id : delete the "id" cscpUserDetail.
     *
     * @param id the id of the cscpUserDetailDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cscpUserDetails/{id}")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public ResponseEntity<Void> deleteCscpUserDetail(@PathVariable  Integer id) {
        log.debug("REST request to delete CscpUserDetail : {}", id);
        cscpUserDetailService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
    @GetMapping("/cscpUserDetailsOr")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public PageResult<CscpUserDetailDTO> getUserDetailOr(CscpUserDetailLike cscpUserDetailLike, Pageable page) {
        log.debug("REST request to get CscpUserDetails");
        return cscpUserDetailService.findByCscpUserDetailDTOOr(cscpUserDetailLike, page);
    }
    
    @GetMapping("/cscpUserDetailsByUserId")
    @BeanExposeMethodAble(component = ADMIN,method = METHOD)
    public CscpUserDetailDTO getByUserId(Integer userId) {
        log.debug("REST request to get CscpUserDetails by user id");
        return cscpUserDetailService.findByUserId(userId);
    }

}
